package cn.xny.system.controller;


import cn.xny.basic.util.AjaxResult;
import cn.xny.basic.util.PageList;
import cn.xny.system.domain.Dictionarydetail;
import cn.xny.system.query.DictionarydetailQuery;
import cn.xny.system.service.DictionarydetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类：数据字典
 */
@Api(value = "数据字典接口类", description = "处理数据字典的CRUD操作")
@RestController
@RequestMapping("/dictionarydetail")
public class DictionarydetailController {


    @Autowired
    private DictionarydetailService dictionarydetailService;

    /**
    * 查询单个
    */

   @ApiOperation(value = "查询单个")
   @GetMapping("/{id}")
   public Dictionarydetail findOne(@PathVariable("id") Long id) {


        return dictionarydetailService.loadByAll(id);
    }

    /**
     * 查询全部
     *
     * @return
     */
    @ApiOperation(value = "查询全部")
    @GetMapping
    public List findAll () {

        return dictionarydetailService.loadAll();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {


            dictionarydetailService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");

        }

    }

    /**
     * 添加或者修改
     */
    @ApiOperation(value = "添加或者修改")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Dictionarydetail dictionarydetail) {
        try {
            if (dictionarydetail.getId() == null) {
                dictionarydetailService.save(dictionarydetail);
            } else {

                dictionarydetailService.update(dictionarydetail);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();

            return new AjaxResult(false, "操作失败");
        }

    }

    /**
     * 分页加高级查询
     *
     * @param dictionarydetailQuery
     * @return
     */
    @ApiOperation(value = " 分页加高级查询")
    @PostMapping
    public PageList<Dictionarydetail> pageQuery(@RequestBody DictionarydetailQuery dictionarydetailQuery) {

        return dictionarydetailService.pageQuery(dictionarydetailQuery);

    }


    @ApiOperation(value = "批量删除")
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids) {
        try {
            dictionarydetailService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");

        }

    }



}
